package com.lzh.others.basejava.Exception_12;


/**
 * @author by lzh
 * @Classname ExtraFeatures
 * @Description 进一步自定义异常，加入额外的构造器和成员
 * 要记住，使用程序包的客户端程序员可能仅仅只是查看一下抛出的异常类型，所以对异常添加过多其他功能也许根本用不上
 * @Date 2022/6/28 22:26
 */
public class ExtraFeatures {
    public static void f() throws MyException2 {
        System.out.println("Throwing MyException2 from f()");
        throw new MyException2("Originated in f()");
    }

    public static void g() throws MyException2 {
        System.out.println("Throwing MyException2 from g()");
        throw new MyException2("Originated in g()");
    }

    public static void h() throws MyException2 {
        System.out.println("Throwing MyException2 from h()");
        throw new MyException2("Originated in h()", 47);
    }

    public static void main(String[] args) {
        try {
            f();
        } catch (MyException2 myException2) {
            myException2.printStackTrace(System.out);
        }
        try {
            g();
        } catch (MyException2 myException2) {
            myException2.printStackTrace(System.out);
        }
        try {
            h();
        } catch (MyException2 myException2) {
            myException2.printStackTrace(System.out);
        }
    }
}

class MyException2 extends Exception {
    private int x;

    public MyException2() {
    }

    public MyException2(String message) {
        super(message);
    }

    public MyException2(String message, int x) {
        super(message);
        this.x = x;
    }

    public int val() {
        return x;
    }

    /**
     * 覆盖了Throwable 的 getMessage()方法
     *
     * @return
     */
    @Override
    public String getMessage() {
        return "Detail Message: " + x + " " + super.getMessage();
    }
}

